import 'package:flutter/material.dart';
import 'package:flutter_trip/widget/fwebview.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MyPage extends StatefulWidget {
  const MyPage({super.key});

  @override
  State<MyPage> createState() => _MyPageState();
}



class _MyPageState extends State<MyPage> {
  final city = {
    '北京':['东城区','西城区','北城区','朝阳区','北海','日本','小日本','打底','lol','心急的','豆腐'],
    '上海':['东城区','西城区','北城区','朝阳区','北海','日本','小日本','打底','lol','心急的','豆腐'],
    '杭州':['下城区','余杭区','萧山区'],
  };
  List<String> cityList =  ['东城区','西城区','北城区','朝阳区','北海','日本','小日本','打底','lol','心急的','豆腐'];

  ScrollController _scrollController = ScrollController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController.addListener(() {
      if(_scrollController.position.pixels == _scrollController.position.maxScrollExtent){
        _loadMore();
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FWebView(url:'https://m.ctrip.com/webapp/myctrip/',hideAppBar: true,statusBarColor: '4c5bca',),
      // body: ListView(
      //   children: _buildList(),
      // )
      // body: RefreshIndicator(
      //   onRefresh: _refesh,
      //   child: GridView.count(
      //     controller: _scrollController,
      //     crossAxisCount: 2,
      //     children: _buildGridList(),
      //   ),
      //
      // )
    );
  }

  Future<Null> _refesh() async{
   await Future.delayed(Duration(seconds: 2));
    setState(() {
      cityList =  cityList.reversed.toList();
    });
  }

  Future<Null> _loadMore() async{
    await Future.delayed(Duration(seconds: 1));
    setState(() {
      List<String> list = List<String>.from(cityList);
      list.addAll(cityList);
      cityList = list;
    });
  }

  List<Widget> _buildGridList(){
    return cityList.map((e) {
       return Container(
         margin: EdgeInsets.all(2),
         height: 80,
         color: Colors.blue,
         child: Text(e),
       );
    }).toList();


  }


  List<Widget> _buildList(){
     List<Widget> list = [];
     city.keys.forEach((element) {
      list.add(_items(element,city[element] ?? []));
    });
     return list;
  }
  Widget _items(String name, List<String> list){
     return ExpansionTile(
         title: Text(name),
         children: list.map((e) => _subItems(e)).toList(),
     );
  }

  Widget _subItems(String name){
    return FractionallySizedBox(
      widthFactor: 1,
      child: Container(
        height: 50,
        child: Text(name),
      )
    );
  }

}
